const { NODE_ENV, VUE_APP_OPEN_GZIP, VUE_APP_SPLIT_CHUNK } = process.env,
  path = require('path'),
  resolve = (dir) => path.join(__dirname, dir),
  TerserPlugin = require('terser-webpack-plugin'),
  CompressionPlugin = require('compression-webpack-plugin');
module.exports = {
  publicPath: NODE_ENV === 'development' ? '/' : './',
  outputDir: 'dist',
  assetsDir: 'static',
  indexPath: 'index.html',
  productionSourceMap: false,
  lintOnSave: false,
  css: {
    extract: {
      filename: '[name].[hash:9].css',
    },
    loaderOptions: {
      less: {
        javascriptEnabled: true,
      },
    },
  },
  devServer: {
    port: 3000,
  },
  configureWebpack: (config) => {
    config.name = 'vue2.6-cli4模板';

    if (VUE_APP_OPEN_GZIP) {
      config.plugins.push(
        new CompressionPlugin({
          algorithm: 'gzip',
          test: /\.js$|\.html$|\.css/,
          threshold: 10240,
          minRatio: 0.8,
        })
      );
    }
  },
  chainWebpack: (config) => {
    config.when(VUE_APP_SPLIT_CHUNK, (config) => {
      // split
      config.optimization.splitChunks({
        chunks: 'all', //指定哪些模块需要打包
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial', // only package third parties that are initially dependent
          },
          antdv: {
            name: 'chunk-ant-design-vue', // split ant-design-vue into a single package
            priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
            test: /[\\/]node_modules[\\/]_?ant-design-vue(.*)/, // in order to adapt to cnpm
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'), // can customize your rules
            minChunks: 3, // 被引用3次就提取出来
            priority: 5,
            reuseExistingChunk: true, // 表示是否使用已有的 chunk，如果为 true 则表示如果当前的 chunk 包含的模块已经被抽取出去了，那么将不会重新生成新的。
          },
        },
      });
    });
  },
};
